********************************************************************************
*********Calculate workplace and individvidual fixed effects from AKM model*****
********************************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

clear
gen drop = .
save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\wage_panel.dta", replace

forvalues year =1995/2015{
 
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	ren peorgnr_lopnr orgnr
	gen year = `year'	 
		drop if orgnr==.
		drop if forvers==0 | forvers==.
			if year>2000 {
		keep  lopnr year orgnr cfarnr_lopnr ssyk* ast* forvers sektorkod examar sun2000niva
		}
				if year<=2000 {
		keep  lopnr year orgnr cfarnr_lopnr   ast*   forvers sektorkod examar sun2000niva
		}
	joinby lopnr year orgnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\sls_lim.dta"  , unmatched(master)

			
	if year>2000 & year <2005 {
	gen ssyk_sls=ssyk
		replace ssyk = ssyk4 if ssyk4!="" & ssyk4!="****" 
	}
	if year>2004 {
gen ssyk_sls=ssyk
	drop ssyk
		ren ssyk4 ssyk 
	
				
	}
	if year < 2002{
	ren astsni92 sni
	}
	if year>2001 & year <2007{
	ren astsni2002 sni
	}
	if year>2006 {
	ren astsni2007 sni
	}
		if year>2000 & year <2016 {
		gen ssyk3 = substr(ssyk,1,3)
		gen ssyk3_sls = substr(ssyk_sls,1,3)
		keep  sni  orgnr ssyk3*  lopnr    year cfar* manl   examar sun2000niva forvers sektorkod
	}
	
			if  year <=2000 {
		
		keep orgnr sni  lopnr  year cfar* manl examar sun2000niva forvers sektorkod
	}


append using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\wage_panel.dta",
compress 
save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\wage_panel.dta", replace
}

 cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö"
	 	joinby lopnr using  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\by_sex.dta",
	
	gen age = year-fodelsear
	drop if age>64



	save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\wage_panel.dta", replace
	
	do "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\pbb year.do"

	drop if forvers<(pbb/100) |forvers==.
	
	
*drop if cfar_sls!= cfarnr_lopnr & cfar_sls!=.
duplicates tag lopnr year orgnr, gen (dup)

duplicates drop orgnr lopnr year, force
drop if dup >0 & ssyk3!= ssyk3_sls & ssyk3_sls!=""
drop if dup!=0


drop  ssyk3_sls
foreach var in manl forvers{
	replace `var'= `var'/0.977369 if year==1995
	replace `var'= `var'/0.981972 if year==1996
	replace `var'= `var'/0.986958 if year==1997
	replace `var'= `var'/0.985807 if year==1998
	replace `var'= `var'/0.990027 if year==1999
	replace `var'= `var'/1 if year==2000
	replace `var'= `var'/1.024549 if year==2001
	replace `var'= `var'/1.046414 if year==2002
	replace `var'= `var'/1.066743 if year==2003
	replace `var'= `var'/1.070963 if year==2004
	replace `var'= `var'/1.075566 if year==2005
	replace `var'= `var'/1.090219 if year==2006
	replace `var'= `var'/1.114346 if year==2007
	replace `var'= `var'/1.153088 if year==2008
	replace `var'= `var'/1.149444 if year==2009
	replace `var'= `var'/1.1641 if year==2010
	replace `var'= `var'/1.1945 if year==2011
	replace `var'= `var'/1.205 if year==2012
	replace `var'= `var'/1.2045 if year==2013
	replace `var'= `var'/1.2024 if year==2014
	replace `var'= `var'/1.2019 if year==2015
}
gen ln_wage=ln( manl)
gen ln_inc=ln( forvers)
drop manl forvers
keep if year<2011
drop if ln_wage==. & ln_inc==.
gen age_cat=int(age/5)

 
 
foreach samp in full woman man {
preserve
    keep if ln_wage!=.
	gen full=1
	sum full
	gen woman =kon=="2"
	gen man=woman==0
	keep if `samp'==1
	bysort lopnr: egen count=count(lopnr)
	drop if count <2
	replace count=1
	collapse(sum) count , by ( cfarnr_lopnr year )
	bysort  cfarnr_lopnr: egen sum_nonsing=sum(count)
	drop if  sum_nonsing<90
	bysort   cfarnr_lopnr: egen msize=mean(count)
	drop if  msize<15
	keep   cfarnr_lopnr year
	duplicates drop cfarnr_lopnr , force
	save `samp'_akm_firm_id, replace
	restore
}


preserve
keep if ln_wage!=.
gen full=1
gen woman =kon=="2"
gen man=woman==0
bysort lopnr: egen count=count(lopnr)
drop if count <2

collapse(sum) full woman man , by ( cfarnr_lopnr year )
	
foreach samp in full woman man {	
	bysort   cfarnr_lopnr: egen msize_`samp'=mean( `samp')
	foreach num in 1 5 10 15 20 25{
		gen `samp'_s`num'= msize_`samp'>= `num' &  msize_`samp'!=.
   }
}

collapse (max)  full_s* woman_s* man_s* , by(cfarnr_lopnr)
save akm_wagesamp, replace
restore

preserve
keep if ln_inc!=.
gen full=1
gen woman =kon=="2"
gen man=woman==0

bysort lopnr: egen count=count(lopnr)
drop if count <2

collapse(sum) full woman man , by ( cfarnr_lopnr year )
	
foreach samp in full woman man {
	bysort   cfarnr_lopnr: egen msize_`samp'=mean( `samp')
	foreach num in 1 5 10 15 20 25{
		gen `samp'_si`num'= msize_`samp'>= `num' &  msize_`samp'!=.
	}
}
collapse (max)  full_si* woman_si* man_si* , by(cfarnr_lopnr)
save akm_incsamp, replace
restore
gen woman =kon=="2"
joinby lopnr year using "\\micro.intra\Projekt\P0844$\P0844_Gem\jämställdhetsplaner\data\LISA_90_15_experience.dta", unmatched(master)
replace sun2000niva="" if sun2000niva=="*" | sun2000niva=="-"
destring sun2000niva, replace force
replace  sun2000niva=. if sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if sun2000niva>=100 & sun2000niva<200
replace educ_year= 9.4 if sun2000niva>=200 & sun2000niva<300
replace educ_year= 11.2 if sun2000niva>=300 & sun2000niva<330
replace educ_year= 12.4 if sun2000niva>=330 & sun2000niva<400
replace educ_year= 14.2 if sun2000niva>=410 & sun2000niva<530
replace educ_year= 17 if sun2000niva>=530 & sun2000niva<600
replace educ_year= 20.4 if sun2000niva>=600 & sun2000niva<=999	
	
drop sun2000niva
gen agesq= age^2 
tab educ_year, gen (eddum)
drop eddum3
foreach l in 1 2 4 5 6 7{
	gen ed`l'_age=eddum`l'*age 
	gen ed`l'_agesq=eddum`l'*agesq 
}

preserve 
clear 
set obs 1 
gen slang=.
save firm_fe_wage, replace
restore
foreach num in 1 5 15   {
	preserve 
	drop _merge
	joinby  cfarnr_lopnr using akm_wagesamp, unmatched(master)
	keep if full_s`num'==1
	keep   ln_wage age agesq- ed7_agesq lopnr full_s* woman_s* man_s* woman cfarnr_lopnr  tenure totexp ssyk3 year
	reghdfe ln_wage age agesq- ed7_agesq if full_s`num'==1, abs(firm_fe_`num'=cfarnr_lopnr   tenure totexp ssyk3 year lopnr )
   	reghdfe ln_wage age agesq- ed7_agesq if woman_s`num'==1 & woman==1, abs(firm_fe_w`num'=cfarnr_lopnr   tenure totexp ssyk3 year  lopnr)
	reghdfe ln_wage age agesq- ed7_agesq if man_s`num'==1 & woman==0, abs(firm_fe_m`num'=cfarnr_lopnr   tenure totexp ssyk3 year  lopnr)
	collapse (max)firm_fe* , by(cfarnr_lopnr )			
	append using firm_fe_wage
	save firm_fe_wage, replace
	restore
	}
preserve 
clear 
set obs 1 
gen slang=.
save ind_fe_wage, replace
restore
foreach num in 1 5 15   {
	preserve
	drop _merge
	joinby  cfarnr_lopnr using akm_wagesamp, unmatched(master)
	keep if full_s`num'==1
	keep   ln_wage age agesq- ed7_agesq  full_s* woman_s* man_s* woman cfarnr_lopnr  tenure totexp ssyk3 year lopnr
	reghdfe ln_wage age agesq- ed7_agesq if full_s`num'==1, abs(cfarnr_lopnr   tenure totexp ssyk3 year  ind_fe_`num'=lopnr)
	collapse (max)ind_fe* , by(lopnr )
	append using ind_fe_wage
	save ind_fe_wage, replace
	restore			
  }
	
preserve 
clear 
set obs 1 
gen slang=.
save firm_fe_inc, replace
save ind_fe_inc, replace
restore
foreach num in 1 5 15 {
preserve 
	drop _merge
	joinby  cfarnr_lopnr using akm_incsamp, unmatched(master)
	keep if full_si`num'==1
	keep   ln_inc age agesq- ed7_agesq  full_s* woman_s* man_s* woman lopnr cfarnr_lopnr lopnr  tenure totexp ssyk3 year
    reghdfe ln_inc age agesq- ed7_agesq if full_si`num'==1, abs(firm_fei_`num'=cfarnr_lopnr  tenure totexp ssyk3 year  ind_fei_`num'=lopnr)
	reghdfe ln_inc age agesq- ed7_agesq if woman_si`num'==1 & woman==1, abs(firm_fei_w`num'=cfarnr_lopnr  tenure totexp ssyk3 year  lopnr)
	reghdfe ln_inc age agesq- ed7_agesq if man_si`num'==1 & woman==0, abs(firm_fei_m`num'=cfarnr_lopnr  tenure totexp ssyk3 year lopnr)
	collapse (max)firm_fei* , by(cfarnr_lopnr )
	append using firm_fe_inc
	save firm_fe_inc, replace
	restore
	preserve
	drop _merge
	joinby  cfarnr_lopnr using akm_wagesamp, unmatched(master)
	keep if full_s`num'==1
	keep   ln_wage age agesq- ed7_agesq  full_s* woman_s* man_s* woman cfarnr_lopnr lopnr  tenure totexp ssyk3 year
	reghdfe ln_wage age agesq- ed7_agesq if full_s`num'==1, abs(cfarnr_lopnr   tenure totexp ssyk3 year  ind_fei_`num'=lopnr)
	collapse (max)ind_fe* , by(lopnr )
	append using ind_fe_inc
	save ind_fe_inc, replace
	restore
}

********************************************************************************
***Generate variables to connect workplace and organizations over time cleans **
***data frommergers etc. Uses Statstics Sweden's principles for the FAD-coding**
********************************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_1990.dta", clear

keep lopnr   peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol arbstid_lopnr instkod*

gen year=1990

save temp, replace
forvalues year =1991/2013{
 use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
keep  lopnr peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol arbstid_lopnr instkod*
gen year=`year'
append using temp
save temp, replace
compress
}
use temp, clear
gen unem =(ampol>0 & ampol!=.) | (arblos>0 & arblos!=.) 
drop ampol arblos

compress 
save temp, replace

forvalues year =2014/2015{
 use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear

keep  lopnr peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol  instkod*

gen year=`year'
compress
append using temp
save temp, replace
}
compress

keep cfar year 

gen ant_anst=1
ren cfar cid1
collapse (sum) ant , by (cid year)
sort  cid1 year
gen kvar_cfar = cid1==cid1[_n+1] & year == year[_n+1]-1 if year!=2015
gen ant_anst2 = ant_anst[_n+1] if kvar_cfar==1
gen kvar_cfar2 = cid1 == cid1[_n-1] & year == year[_n-1]+1 if year!=1990
gen ant_anstlag = ant_anst[_n-1] if kvar_cfar2==1
ren ant_anst ant_anst1
gen cid2 =cid1
save cid_link, replace

clear
gen drop = .
save fad1_ftg1, replace

clear
gen drop = .
save fad2_ftg2, replace

clear
gen drop = .
save fad1_arb1, replace

clear
gen drop = .
save fad2_arb2, replace


 forvalues year=1990/2015{
 use temp, clear
 keep if year==`year' | year==`year'+1
 
 preserve 
 keep if year==`year'
 keep peorgnr_lopnr
 ren peorgnr_lopnr fid1
 duplicates drop fid1, force
 save fid1, replace 
restore
 preserve 
 keep if year==`year'
 keep cfarnr_lopnr
 ren cfarnr_lopnr cid1
 duplicates drop cid1, force
 save cid1, replace 
 restore
 
 preserve 
 keep if year==`year'+1
 keep peorgnr_lopnr
 ren peorgnr_lopnr fid2
 duplicates drop fid2, force
 save fid2, replace 
restore

 preserve 
 keep if year==`year'+1
 keep cfarnr_lopnr
 ren cfarnr_lopnr cid2
 duplicates drop cid2, force
 save cid2, replace 
restore
 
 
gen jur= substr(instkod, 4, 2)
replace jur= substr(instkod6, 5, 2) if jur==""
replace jur= substr(instkod7, 6, 2) if jur==""
gen person1=jur=="10"
drop inst* jur
 gen count=1
ren peorgnr_lopnr fid1
keep if fid1!=.
 sort lopnr year
gen fid2=fid[_n+1] if lopnr==lopnr[_n+1]
egen cfartag=tag (cfarnr_lopnr year)
bysort fid1 year: egen no_cfar1=sum(cfartag) 
 sort lopnr year
gen  no_cfar2=no_cfar1[_n+1] if lopnr==lopnr[_n+1]
gen  person2=person1[_n+1] if lopnr==lopnr[_n+1]
ren cfarnr_lopnr cid1
gen cid2=cid[_n+1] if lopnr==lopnr[_n+1]

keep if  fid2!=.
bysort fid1: egen t1=sum(count)
bysort fid2: egen t2=sum(count)
preserve
drop if t1<3 | t2<3
collapse (sum)count (max) t1 t2, by(fid1 fid2) 
ren count g
gen k1= g/t1>.5 
gen k2= g/t2>.5 
gen k12 = k1==1 & k2==1
gen kvar1=""
gen kvar2=""
replace kvar2="11" if k12==1
replace kvar1="11" if k12==1
gen uind = k1==0 & k2==1
replace kvar2="01" if uind==1
bysort fid1: egen u=max(uind)
gen ukvar= k12==1 & u==1

gen g_neg=-g

bysort fid1: egen g1max= rank(g_neg), unique

gen uned = k12==0 & u==1  & g1max==1
replace kvar1 ="50" if uned==1

gen hind= k1==1 & k2==0
replace kvar1 ="10" if hind==1
bysort fid2: egen h=max(hind)
gen hkvar= k12==1 & h==1

bysort fid2: egen g2max= rank(g_neg), unique
gen hny= k12==0 & h==1 &  g2max==1
replace kvar2 ="05" if hny==1

save makro, replace
restore 

preserve
keep if t1<3 | t2<3
replace cid1=. if no_cfar1!=1
replace cid2=. if no_cfar2!=1
collapse (sum)count (max) t1 t2 no_cfar* cid* person* , by(fid1 fid2) 
ren count g

gen kvar_mik1 = cid1==cid2 & fid1==fid2 & person1==1 & person2==1 & cid1!=. 
gen kvar_mik2 = cid1==cid2 & t2>=1 & t2!=. & cid1!=.
gen kvar_mik3 = g==t1 & g==t2 
egen kvar_mik= rowmax(kvar_mik1 kvar_mik2 kvar_mik3)
append using makro
save ftg, replace
restore

keep if  cid2!=.
drop t1 t2
bysort cid1: egen t1=sum(count)
bysort cid2: egen t2=sum(count)

preserve
drop if t1<3 | t2<3
collapse (sum)count (max) t1 t2 person* fid1 fid2 no_cfar1 no_cfar2, by(cid1 cid2) 
ren count g
gen k1= g/t1>.5 
gen k2= g/t2>.5 

gen k12 = k1==1 & k2==1


gen kvar1="11" if cid1==cid2 & cid2!=.
replace kvar1="11" if k12==1
replace kvar1="11" if fid1==fid2 & no_cfar1==1 & no_cfar2==1

gen kvar2= kvar1

gen uind = k1==0 & k2==1
replace kvar2="01" if uind==1
bysort cid1: egen u=max(uind)

gen ukvar= k12==1 & u==1

gen g_neg=-g

bysort cid1: egen g1max= rank(g_neg), unique

gen uned = k12==0 & u==1  & g1max==1
replace kvar1 ="50" if uned==1

gen hind= k1==1 & k2==0
replace kvar1 ="10" if hind==1
bysort fid2: egen h=max(hind)
gen hkvar= k12==1 & h==1

bysort fid2: egen g2max= rank(g_neg), unique
gen hny= k12==0 & h==1 &  g2max==1
replace kvar2 ="05" if hny==1

save makro_arb, replace
restore 


preserve
keep if t1<3 | t2<3
replace fid1=. if no_cfar1!=1
replace fid2=. if no_cfar2!=1
collapse (sum)count (max) t1 t2 no_cfar* fid* person* , by(cid1 cid2) 

ren count g



gen kvar_mik1 = cid1==cid2 & fid1==fid2 & person1==1 & person2==1  
gen kvar_mik2 = cid1==cid2 
gen kvar_mik3 = g==t1 & g==t2 
egen kvar_mik= rowmax(kvar_mik1 kvar_mik2 kvar_mik3)

append using makro_arb
save arb, replace
restore

use "ftg.dta" , clear


cd  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

gen kvar_d = kvar1=="11"
bysort fid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik1==1 & tempkvar==0
drop tempkvar
bysort fid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik2==1 & tempkvar==0
drop tempkvar
bysort fid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik3==1 & tempkvar==0
drop tempkvar
gen kvar1_fid2= fid2 if kvar_d==1

bysort fid1: egen tempkvar= sum(kvar_d)

gen ned= tempkvar==0
gen ned_upp = kvar1=="50"
gen ned_hop = kvar1=="10"
gen ned_hop1_fid2 = fid2 if kvar1=="10"
gen kvar_upp= tempkvar==1 & u==1

collapse (max) kvar_d  kvar1_fid2 kvar_upp ned*, by(fid1)

gen kvar1="11" if kvar_d==1
replace kvar1="10" if ned_hop==1
replace kvar1="50" if ned_upp==1
replace kvar1="20" if kvar1==""
joinby fid1 using fid1, unmatched(both)
replace kvar1="30" if kvar1==""
replace kvar_d=0 if kvar_d==.
replace ned=1 if ned==.
ren kvar_d kvar_d1
gen year=`year'
append using fad1_ftg1
drop _merge
save fad1_ftg1, replace


use "ftg.dta" , clear


gen kvar_d = kvar1=="11"
bysort fid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik1==1 & tempkvar==0
drop tempkvar
bysort fid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik2==1 & tempkvar==0
drop tempkvar
bysort fid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik3==1 & tempkvar==0
drop tempkvar
gen kvar2_fid1= fid1 if kvar_d==1

bysort fid2: egen tempkvar= sum(kvar_d)

gen ny= tempkvar==0
gen ny_hop = kvar2=="05"
gen ny_upp = kvar2=="01"
gen ny_upp_fid1= fid1 if ny_upp==1
gen kvar_ny= tempkvar==1 & h==1
collapse (max) kvar_d  kvar2_fid1 kvar_ny ny*, by(fid2)

gen kvar2="11" if kvar_d==1
replace kvar2="01" if ny_upp==1
replace kvar2="05" if ny_hop==1
replace kvar2="02" if kvar2==""
joinby fid2 using fid2, unmatched(both)
replace kvar2="03" if kvar2==""
replace kvar_d=0 if kvar_d==.
replace ny=1 if ny==.
ren kvar_d kvar_d2
gen year=`year'+1
append using fad2_ftg2
drop _merge
save fad2_ftg2, replace


use "arb.dta" , clear
cd  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"
gen year=`year'
gen kvar_d = cid1==cid2 & t1>=3 & t2>=3  & cid2!=.
bysort cid1: egen tempkvar= sum(kvar_d)

replace kvar_d =1 if k1==1 & k2==1 & tempkvar==0 
drop tempkvar
bysort cid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik2==1 & tempkvar==0
drop tempkvar
bysort cid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik1==1 & tempkvar==0
drop tempkvar
bysort cid1: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik3==1 & tempkvar==0
drop tempkvar
bysort cid1: egen tempkvar= sum(kvar_d)
gen kvar1_cid2= cid2 if kvar_d==1
gen ned= tempkvar==0
gen ned_upp = kvar1=="50" & ned==1
gen ned_hop = kvar1=="10" & ned==1
gen ned_hop1_cid2 = cid2 if kvar1=="10" & & ned==1
gen kvar_upp= tempkvar==1 & u==1
replace kvar_d =tempkvar

collapse (max) kvar_d  kvar1_cid2 kvar_upp ned*, by(cid1)

gen kvar1="11" if kvar_d==1
replace kvar1="10" if ned_hop==1
replace kvar1="50" if ned_upp==1
replace kvar1="20" if kvar1==""
joinby cid1 using cid1, unmatched(both)
replace kvar1="30" if kvar1==""
replace kvar_d=0 if kvar_d==.
replace ned=1 if ned==.
ren kvar_d kvar_d1
gen year=`year'
append using  fad1_arb1
drop _merge
save fad1_arb1, replace
use "arb.dta" , clear

cd  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"



gen kvar_d = cid1==cid2 & t1>=3 & t2>=3  & cid2!=.
bysort cid2: egen tempkvar= sum(kvar_d)

replace kvar_d =1 if k1==1 & k2==1 & tempkvar==0 
drop tempkvar
bysort cid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik2==1 & tempkvar==0
drop tempkvar
bysort cid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik1==1 & tempkvar==0
drop tempkvar
bysort cid2: egen tempkvar= sum(kvar_d)
replace kvar_d =1 if kvar_mik3==1 & tempkvar==0
drop tempkvar
gen kvar2_cid1= cid1 if kvar_d==1
bysort cid1: egen tempkvar= sum(kvar_d)
gen ny= tempkvar==0
gen ny_hop = kvar2=="05" & ny==1
gen ny_upp = kvar2=="01" & ny==1
gen ny_upp_cid2= cid2 if ny_upp==1
gen kvar_ny= tempkvar==1 & h==1
replace kvar_d =tempkvar
collapse (max) kvar_d  kvar2_cid1 kvar_ny ny*, by(cid2)

gen kvar2="11" if kvar_d==1
replace kvar2="01" if ny_upp==1
replace kvar2="05" if ny_hop==1
replace kvar2="02" if kvar2==""
joinby cid2 using cid2, unmatched(both)
replace kvar2="03" if kvar2==""
replace kvar_d=0 if kvar_d==.
replace ny=1 if ny==.
ren kvar_d kvar_d2
gen year=`year'+1
append using  fad2_arb2
drop _merge
save fad2_arb2, replace
 }
******************************************************************
*********Create dummies for workplace survival********************
******************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\

use "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp.dta", clear
keep cfar year 
gen size= 1
collapse (sum) size , by(cfar year) 
ren cfar cid1
gen cid2=cid1
joinby cid1 year using fad1_arb1, unmatched(master) _merge(merge2)
joinby cid2 year using fad2_arb2, unmatched(master) _merge(merge3)
sort cid1 year 
gen wp_surv = 1 if kvar_d1==1 & kvar1_cid2==cid1
forvalues n=1/7{
	local v=`n'+1
	replace wp_surv=`v' if wp_surv==`n' &  kvar_d1[_n+`n']==1 & kvar1_cid2[_n+`n']==cid1 &  cid1[_n+`n']==cid1 & year[_n+`n']==(year+`n')
}

gen wp_survn = 1 if kvar_d2==1 & kvar2_cid1==cid1
sort cid1 year 
forvalues n=2/7{
	local v=`n'-1
	replace wp_survn=`n' if wp_survn==`v' &  kvar_d2[_n-`v']==1 & kvar2_cid1[_n-`v']==cid1 &  cid1[_n-`n']==cid1 & year[_n-`v']==(year-`v')
}


keep cid1 year wp_surv  wp_survn
save wp_surv, replace
******************************************************************
*********Create variable for workplace sex composition************
******************************************************************

cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

use wage_panel, clear

keep if orgnr!=. & cfarnr_lopnr!=.
keep lopnr orgnr cfarnr_lopnr year kon 
gen wom =kon=="2"
drop kon 
duplicates drop lopnr year, force
gen count=1
bysort cfarnr_lopnr year: egen no_emp=sum(count)
bysort cfarnr_lopnr year: egen no_wom=sum(wom)
gen workpl_w= (no_wom-1)/(no_emp-1) if wom==1
replace workpl_w= (no_wom)/(no_emp-1) if wom==0
gen workpl_m=1-workpl_w
joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta", unmatched(none)
save "\\micro.intra\projekt\P0844$\P0844_Gem\Arbetsmiljö\woman_wp_lim.dta", replace
  
******************************************************************
*********Create data having children and the children's age*******
******************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

 use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Barn.dta" , clear
ren lopnr par
ren barn lopnr
joinby  lopnr using "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Bakgrund.dta" , unmatched(master)
keep par fodelsear
ren par lopnr
destring fodelsear, replace 
bysort lopnr: egen child_no=rank(fod), unique
keep if child_no<6
ren fodelse by_ch
reshape wide by_ch@, i(lopnr) j(child_no)
save "\\micro.intra\projekt\P0844$\P0844_Gem\Arbetsmiljö\child_by.dta", replace
 
******************************************************************
*********Create data on workplace transitions********************
******************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"
use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_1990.dta", clear

keep lopnr   peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol arbstid_lopnr instkod*
joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta", unmatched(none)
gen year=1990

save temp, replace
forvalues year =1991/2013{
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	keep  lopnr peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol arbstid_lopnr instkod*
	joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta", unmatched(none)
	gen year=`year'
	append using temp
	save temp, replace
}
use temp, clear
gen unem =(ampol>0 & ampol!=.) | (arblos>0 & arblos!=.) 
drop ampol arblos

compress 
save temp, replace

forvalues year =2014/2015{
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	keep  lopnr peorgnr_lopnr cfarnr_lopnr forvers forvink sektorkod arblos ampol  instkod*
	joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta", unmatched(none)
	gen year=`year'
	append using temp
	save temp, replace
}
compress

do "pbb year.do"
joinby lopnr using  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\by_sex.dta",
gen age = year-fodelsear
drop if age>64
gen full=1
gen woman =kon=="2"
gen man=woman==0
	

foreach var in  forvers{
replace `var'= `var'/0.977369 if year==1995
replace `var'= `var'/0.981972 if year==1996
replace `var'= `var'/0.986958 if year==1997
replace `var'= `var'/0.985807 if year==1998
replace `var'= `var'/0.990027 if year==1999
replace `var'= `var'/1 if year==2000
replace `var'= `var'/1.024549 if year==2001
replace `var'= `var'/1.046414 if year==2002
replace `var'= `var'/1.066743 if year==2003
replace `var'= `var'/1.070963 if year==2004
replace `var'= `var'/1.075566 if year==2005
replace `var'= `var'/1.090219 if year==2006
replace `var'= `var'/1.114346 if year==2007
replace `var'= `var'/1.153088 if year==2008
replace `var'= `var'/1.149444 if year==2009
replace `var'= `var'/1.1641 if year==2010
replace `var'= `var'/1.1945 if year==2011
replace `var'= `var'/1.205 if year==2012
replace `var'= `var'/1.2045 if year==2013
replace `var'= `var'/1.2024 if year==2014
replace `var'= `var'/1.2019 if year==2015

}

joinby lopnr year using  "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\woman_wp_lim.dta", unmatched(master)
gen man_wp=1-woman_wp
gen emp = forvers>(pbb/100) & forvers!=.
gen unem =(ampol>0 & ampol!=.) | (arblos>0 & arblos!=.) 
gen un_ne=emp!=1 | unem==1
gen cid1= cfarnr_lopnr
drop _merge
joinby year cid1 using fad1_arb1_lim, unmatched(master)
drop _merge
gen cid2= cfarnr_lopnr
joinby year cid2 using fad2_arb2_lim, unmatched(master)

drop _merge	
joinby  cfarnr_lopnr  using firm_fe_wage, unmatched(master)
drop _merge		
joinby year cid1 using wp_surv, unmatched(master)

gen  wpch_ee_t=.
sort lopnr year
forvalues n=1/8{
local v=`n'-1
	replace wpch_ee_t= `n' if   wpch_t== `n' &  emp[_n+`n']==1 & emp[_n+`v']==1
}
	gen  wpch_ee_nt=.
sort lopnr year
forvalues n=1/5{
local v=`n'-1
	replace   wpch_ee_nt= `n' if wpch_nt== `n'  & emp[_n-`n']==1 & emp[_n-`v'] ==1
	}

foreach var in   wpch_ee  {
	forvalues n=1/8{
		local v=`n'-1
		gen `var'_d`n' =   `var'_t<=`n'   if   wp_surv>=`n'& wp_surv!=.
	} 
	forvalues n=1/5{
		gen `var'_dn`n' =   `var'_nt<=`n'   if   wp_survn>=`n'& wp_survn!=.
	} 
}

sort lopnr year
foreach var in  man_wp  firm_fe_5 {
	gen  `var'_chwp=.
	forvalues n=1/8{
		local v=`n'-1
		replace  `var'_chwp= `var'[_n+`n']-`var'[_n+`v'] if  	wpch_ee_t==`n'  
	}
}

foreach var in  man_wp  firm_fe_5 {
	gen `var'_chneg = `var'_chwp<0 if `var'_chwp!=.
}
drop _merge
foreach var in ln_wage man_wp   firm_fe_5 {
	forvalues n=1/8{
		local v=`n'-1
		gen  `var'_chneg_`n'= `var'_chneg*wpch_ee_d`n'  
		replace `var'_chneg_`n'=0 if  wpch_ee_d`n'!=. &`var'!=. & `var'[_n+`n']!=. &    `var'_chneg_`n'==.
	}
 }
 
preserve 
keep    wpch_d* wpch_ee* firm_fe_5   lopnr year ln_wage_chneg_3 man_wp_chneg_3 firm_fe_5_chneg_3
save new_switch, replace
restore

******************************************************************
*********Create data used for weights********************
******************************************************************
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"

use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_1990.dta", clear

keep lopnr astsni92 sun2000grp sun2000niva syssstat*  peorgnr_lopnr cfarnr_lopnr forvers

gen year=1995
ren ast sni
save temp, replace
forvalues year =1997(2)1999{
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	keep lopnr astsni92  sun2000niva sun2000grp syssstat* peorgnr_lopnr cfarnr_lopnr forvers
	gen year=`year'
	ren ast sni
	append using temp
	save temp, replace
}

use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_2001.dta", clear

keep lopnr astsni92   sun2000niva sun2000grp syssstat* ssyk4   peorgnr_lopnr cfarnr_lopnr forvers
gen year=2001
ren ast sni
append using temp
save temp, replace

forvalues year =2003(2)2009{
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	keep lopnr astsni2002 sun2000grp sun2000niva ssyk4  syssstat* peorgnr_lopnr cfarnr_lopnr forvers
	ren ast sni
	gen year=`year'
	append using temp
	save temp, replace
} 
forvalues year =2011(2)2015{
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear

	keep lopnr astsni2007 sun2000grp sun2000niva ssyk4  syssstat* peorgnr_lopnr cfarnr_lopnr forvers
	ren ast sni
	gen year=`year'
	append using temp
	save temp, replace
}
compress
ren peorgnr_lopnr orgnr
joinby lopnr using by_sex, unmatched(none)
gen woman=kon=="2"
gen age =year-fodelsear
gen syss=syssstatj=="1"| syssstat=="1" |syssstat11=="1"

drop  sun2000grp fodelsear  sni kon orgnr cfarnr_lopnr sysss*
joinby lopnr  using birthregion, unmatched(master)
drop _merge
gen birthregion= "europe" if fodelseland_eu28=="EU28 utom Norden"
replace birthregion= "europe"  if fodelseland_eu28=="Europa utom EU28 och Norden"
replace birthregion= "europe"  if fodelseland_eu28=="Norden utom Sverige"
replace birthregion= "sweden"  if fodelseland_eu28=="Sverige"
replace birthregion= "non_europe" if birthregion=="" &  fodelseland_eu28!=""
drop fodelselan* utlsvbakg

foreach var in forvers {
	replace `var'= `var'/(0.977369/1.2019) if year==1995
	replace `var'= `var'/(0.986958/1.2019) if year==1997
	replace `var'= `var'/(0.990027/1.2019) if year==1999
	replace `var'= `var'/(1.024549/1.2019) if year==2001
	replace `var'= `var'/(1.066743/1.2019) if year==2003	
	replace `var'= `var'/(1.075566/1.2019) if year==2005
	replace `var'= `var'/(1.114346/1.2019) if year==2007
	replace `var'= `var'/(1.149444/1.2019) if year==2009
	replace `var'= `var'/(1.1945/1.2019) if year==2011
	replace `var'= `var'/(1.2045/1.2019) if year==2013
}

gen pinc3=1 if pinc<=4
replace pinc3=2 if pinc>=5 & pinc<9
replace pinc3=3 if pinc>=9 & pinc<14

replace ssyk4 =  ssyk4_2012 if year==2015
replace ssyk4 =  "" if ssyk4=="****"

joinby lopnr year using temp_ssyk, unmatched(master)
replace ssyk =  "" if ssyk=="****"
replace ssyk =  "" if ssyk=="***"
replace ssyk4 =ssyk if ssyk4==""
gen ssyk3= substr(ssyk4,1,3)
gen ssyk2= substr(ssyk4,1,2)
gen alder6=1 if age<30
replace alder6=2 if age>=30 & age<40
replace alder6=3 if age>=40 & age<50
replace alder6=4 if age>=50 & age<60
replace alder6=5 if age>=60 & age<70
replace alder6=6 if age>=70 & age!=.

gen alder5=alder6
replace alder5=5 if alder5==6
gen alder3=1 if age<40
replace alder3=2 if age>=40 & age<50
replace alder3=3 if age>=50 & age!=.
replace sun2000niva="" if sun2000niva=="*" | sun2000niva=="-"
destring sun2000niva, replace force
replace  sun2000niva=. if sun2000niva>=999

	
gen edu=.
replace edu= 1 if sun2000niva>=100 & sun2000niva<200
replace edu= 2 if sun2000niva>=200 & sun2000niva<300
replace edu= 3 if sun2000niva>=300 & sun2000niva<330
replace edu= 4 if sun2000niva>=330 & sun2000niva<400
replace edu= 5 if sun2000niva>=410 & sun2000niva<530
replace edu= 6 if sun2000niva>=530 & sun2000niva<600
replace edu= 7 if sun2000niva>=600 & sun2000niva<=999
gen edu2=.
replace edu2= 1 if sun2000niva>=100 & sun2000niva<400
replace edu2= 2 if sun2000niva>=410 & sun2000niva<999
drop  ssyk drop educ_year sun2000niva

egen gen_edu2_alder3_ssyk2 =group(woman edu2 alder3 ssyk2) if syss==1
egen gen_edu_alder5_ssyk2 =group(woman edu alder5 ssyk2)  if syss==1
egen gen_ssyk2 =group(woman ssyk2)  if syss==1
egen gen_edu2_alder3_pinc3 =group(woman edu2 alder3 pinc3) if syss==1
egen gen_edu_alder5_pinc =group(woman edu alder5 pinc)  if syss==1
egen gen_pinc =group(woman pinc)  if syss==1
egen gen_edu_alder5_ssyk3 =group(woman edu alder5 ssyk3)  if syss==1
egen gen_edu_alder5 =group(woman edu alder5)  if syss==1
egen gen_edu2_alder5 =group(woman edu2 alder5)  if syss==1
egen gen_pinc_alder5 =group(woman edu alder5)  if syss==1
egen gen_pinc3_alder5 =group(woman edu2 alder5)  if syss==1
egen gen_edu2_alder3_pinc3_br =group(woman edu2 alder3 pinc3 birthregion) if syss==1
drop forvers ssyk4 ssyk4_2012 _merge ssyk3 alder6
duplicates drop lopnr year, force
compress
bysort year: egen syss_tot=sum(syss)
foreach group of varlist   gen_edu2_alder3_ssyk2-gen_edu2_alder3_pinc3_br  {
	bysort year `group': egen `group'_tot=sum(syss)
 }
 
preserve
keep lopnr year woman syss birthregion pinc pinc3 alder3 edu edu2 alder5 syss_tot gen_*
save weights_arb, replace
